package com.gopay.bis.manager.bank.Wap;

import java.util.Date;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.util.DateUtils;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.abc.ebank.AbcB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 农行wap支付B2C对象封装
 * @author wangxintao
 */
public class AbcWapB2cManager implements WapPayManagerRemoteService {

	@Resource(name = "abcB2cBankServiceClient")
    private BankService abcB2cBankServiceClient;
    
    @Resource(name = "abcB2cBankServiceClientHna")
    private BankService abcB2cBankServiceClientHna;
    
    /**
	 * B2C签名
	 * 
     * @param BaseMessage
     * @return Object
     * @throws GopayException
	 */
    public Object sign(BaseMessage message) throws GopayException {
    	AbcB2cMessage messageB2C = (AbcB2cMessage) message;
		Date orderDate = DateUtils.strToDate(messageB2C.getOrderDate());
		messageB2C.setTrnxDate(DateUtils.format(orderDate, "yyyy/MM/dd"));
		messageB2C.setTrnxTime(DateUtils.format(orderDate, "HH:mm:ss"));
		messageB2C.setMerchantRemarks("GopayB2C");
		messageB2C.setResultNotifyURL(messageB2C.getPayRuturnUrl());
		// 调用远程接口请求签名数据
		BankLogUtils.printB2CManagerSignSendLog(messageB2C);		 
		if(messageB2C.isHna()){
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClientHna.sign(messageB2C);			
		}else{
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClient.sign(messageB2C);		
		}
		BankLogUtils.printB2CManagerSignReturnLog(messageB2C);
		return messageB2C;
	}
	/**
	 * B2C验签
	 * 
     * @param BaseMessage
     * @return Object
     * @throws GopayException
	 */
    public Object verify(BaseMessage message) throws GopayException {
		AbcB2cMessage messageB2C = (AbcB2cMessage) message;
		BankLogUtils.printB2CManagerVerifySendLog(messageB2C);
		AbcB2cMessage abcMessageB2C;
		if(messageB2C.isHna()){
			abcMessageB2C = (AbcB2cMessage)abcB2cBankServiceClientHna.verify(messageB2C);			
		}else{
			abcMessageB2C = (AbcB2cMessage)abcB2cBankServiceClient.verify(messageB2C);		
		}
		BankLogUtils.printB2CManagerVerifyReturnLog(messageB2C, abcMessageB2C.isSuccess());
		return abcMessageB2C;
	}

	/**
	 * B2C订单查询
	 * 
     * @param Object
     * @return Object
     * @throws GopayException
	 */
    public Object query(Object message) throws GopayException {
		AbcB2cMessage messageB2C = (AbcB2cMessage) message;
		messageB2C.setQueryDetail("1"); // 1: 详细查询
		BankLogUtils.printB2CManagerQuerySendLog(messageB2C, messageB2C);
		if(messageB2C.isHna()){
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClientHna.query(messageB2C);	
		}else{
			messageB2C = (AbcB2cMessage) abcB2cBankServiceClient.query(messageB2C);	
		} 
		BankLogUtils.printB2CManagerQueryReturnLog(messageB2C);
		return messageB2C;
	}
	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
